<?php

namespace app\admin\common;

use think\exception\Handle;
use think\facade\Log;
use think\Response;
use Throwable;

class ExceptionHandle extends Handle
{
    public function render($request, Throwable $e): Response
    {
        if($e instanceof \think\db\exception\PDOException) {
            $sqlInfo = $e->getData();
            $sqlInfo = $sqlInfo['Database Status'] ?? 'sql未知错误...';

            Log::error($sqlInfo);
        }

        Log::error($e->getMessage());
        Log::error(sprintf("%s in %s: %s",$e->getMessage(), $e->getFile(), $e->getLine()));

        if($request->isAjax()) {
            $data = \app\api\extend\Response::setError('网络错误,请重试');
            return json($data)->code(500);
        }


        /*
        if($request->isJson() or $request->isAjax(true)) {

        }
        */
        return parent::render($request, $e); // TODO: Change the autogenerated stub
    }
}